package org.hades.corelib.base.usecase

import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flowOn

/**
 * 注入协程调度器而不是硬编码
 * flowOn将调度器应用到上流，下方流不受影响，可多次调用，比如viewmodel的viewmodelScope中处理flowOn下游数据时将在
 * Dispatchers.Main上执行
 */
abstract class FlowUseCase<in P: FlowUseCase.RequestValues, R>(private val coroutineDispatcher: CoroutineDispatcher) {
    operator fun invoke(parameters: P): Flow<R> = execute(parameters)
        .flowOn(coroutineDispatcher)


    protected abstract fun execute(parameters: P): Flow<R>

    interface RequestValues
}